package com.springcloudalibaba;

public class ThreadLocalExample {
    private final static ThreadLocal<String> threadLocal = ThreadLocal.withInitial(() -> "default value");

    public static void main(String[] args) throws InterruptedException {
        System.out.println("current Thread name: " + Thread.currentThread().getName() + "; threadLocal value: " + threadLocal.get());

        Thread thread1 = new Thread(() -> {
            threadLocal.set("thread1 value");
            System.out.println("current Thread name: " + Thread.currentThread().getName() + "; threadLocal value: " + threadLocal.get());
        }, "thread1");

        Thread thread2 = new Thread(() -> {
            threadLocal.set("thread2 value");
            System.out.println("current Thread name: " + Thread.currentThread().getName() + "; threadLocal value: " + threadLocal.get());
        }, "thread2");

        thread1.start();
        thread1.join();
        thread2.start();
        thread2.join();
        System.out.println("current Thread name: " + Thread.currentThread().getName() + "; threadLocal value: " + threadLocal.get());
    }
}
